package com.example.demo.arithmetic.algorithm.queue;

import java.util.Arrays;

/**
 * @Description: 数组实现的queue
 * @Author: LLL
 * @Date: 2020-03-12 22:44
 **/
public class ArrayQueue {

    /**
     * 数据
     */
    private int[] array;

    /**
     * 大小
     */
    private int size;

    /**
     * 头部
     */
    private int front;

    /**
     * 尾部
     */
    private int rear;

    /**
     * @Description:构造器
     * @Param: size 大小
     * @return:
     * @Author: liull
     * @Date: 2020/3/12
     */
    public ArrayQueue(int size) {
        this.size = size;
        this.array = new int[size];
        // 指向对列头的前一个位置
        this.front = -1;
        // 指向队列尾的位置
        this.rear = -1;
    }

    /**
     * @Description:构造器
     * @return:
     * @Author: liull
     * @Date: 2020/3/12
     */
    public ArrayQueue() {
        this(8);
    }

    /**
     * @Description: 队列是否已满
     * @return: boolean
     * @Author: liull
     * @Date: 2020/3/12
     */
    public boolean isFull() {
        return rear == size - 1;
    }

    /**
     * 是否已满
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * @Description: 添加元素
     * @Param: e 元素
     * @return: boolean 成功:true
     * @Author: liull
     * @Date: 2020/3/12
     */
    public boolean add(int e) {
        if (isFull()) {
            return false;
        }
        this.rear++;
        array[this.rear] = e;
        return true;
    }

    /**
     * @Description: 移除元素
     * @Param: e 元素
     * @return: boolean 成功:true
     * @Author: liull
     * @Date: 2020/3/12
     */
    /*public boolean remove(int e) {
        if (isEmpty()) {
            return false;
        }
        this.front++;
        array[front + 1] = e;
        return true;
    }*/

    /**
     * @Description: 出队列
     * @Param: * @param
     * @return: int
     * @Author: liull
     * @Date: 2020/3/12
     */
    public int remove() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不可以取数据！");
        }
        this.front++;
        return array[front];
    }

    @Override public String toString() {
        return "ArrayQueue{" +
                "array=" + Arrays.toString(array) +
                ", size=" + size +
                ", front=" + front +
                ", rear=" + rear +
                '}';
    }

    /**
     * @Description: 显示头部数据 不取出来
     * @Param: * @param
     * @return: int
     * @Author: liull
     * @Date: 2020/3/12
     */
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，无数据！");
        }
        return array[front + 1];
    }

}
